class Admin::TasksController < AdminController
  def index
    @tasks = Task.all
  end

  def new
    @task = Task.new
  end

  def create
    @task = Task.new(params[:task])
    if @task.save
      respond_to do |wants|
        wants.js{
          render :update do |page|
            page.insert_html :bottom, 'milestone_tasks', :partial => "admin/tasks/show", :locals => {:task => @task, :show_actions => true, :row_wrap => true}
            page << "$j('#task_#{@task.id.to_s}').effect('pulsate', { times:1 }, 500);"
            page << "$j(\"#task_form\")[0].reset();"
          end
        }
      end
    else
      render :action => 'new'
    end
  end

  def edit
    @task = Task.find(params[:id])
  end

  def update
    @task = Task.find(params[:id])
    if @task.update_attributes(params[:task])
      respond_to do |wants|
        wants.js{
          render :update do |page|
            page.replace_html 'task_' + @task.id.to_s, :partial => "admin/tasks/show", :locals => {:task => @task, :show_actions => true, :row_wrap => false}
            page << "$j('#task_#{@task.id.to_s}').effect('pulsate', { times:1 }, 500);"
            page << "$j('#task_form_row').html(\"\")"
          end
        }
      end
    else
      render :action => 'edit'
    end
  end

  def destroy
    @task = Task.find(params[:id])
    element_id = @task.id
    @task.destroy
    respond_to do |wants|
      wants.js{
          render :update do |page|
            page << "$j(\"#task_#{element_id}\").fadeOut();"
          end
        }
    end
  end
  
  def show_remote_form
    @task       = (params[:id] == "new" ? Task.new : Task.find(params[:id]))
    @milestone  = Milestone.find params[:milestone_id]
    render :partial => "remote_form", :locals => {:task => @task, :milestone => @milestone}
  end
  
  def show
    @task = Task.find(params[:id])
    render :partial => "show", :locals => {:task => @task}
  end
end